﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Slb.Ocean.Petrel;
using Public.Ocean.Petrel.Extensions.DomainObject;
using Slb.Ocean.Core;
using Slb.Ocean.Petrel.DomainObject;
using Slb.Ocean.Petrel.IO.Attributes;

namespace Public.Ocean.Petrel.Extensions.Attributes
{
    public static class ProjectAttributeExtensions
    {
        public static IEnumerable<TAttributes> AttributeSelect<TAttributes>(this Project project, Func<TAttributes, bool> predicate)
            where TAttributes : CustomAttributesBase, new()
        {
            var service = CoreSystem.GetService<ICustomAttributeService>();
            var source = DataManager.DataSourceManager.GetSource("OceanPetrelAttributeDataSource") as AttributeDataSource;
            if (source != null)
            {
                foreach (var container in source.GetData<AttributeData>())
                {
                    TAttributes attributes = new TAttributes();
                    CustomAttributesBase.Initialize(
                        attributes, container.DomainObjectDroid, 
                        container.DataSourceManager, 
                        service,
                        CustomAttributeEventService.Instance);
                    if (predicate(attributes))
                    {
                        yield return attributes;
                    }
                }
            }
        }

        public static IEnumerable<TAttribute> AttributeSelect<TAttribute>(this Project project)
            where TAttribute : CustomAttributesBase, new()
        {
            return AttributeSelect<TAttribute>(project, attributes => true);
        }
    }
}
